package com.ywb.edugui.dao.facotry;

import com.ywb.edugui.dao.*;
import com.ywb.edugui.dao.impl.OssDaoImpl;
import com.ywb.edugui.dao.impl.SubjectDaoImpl;
import com.ywb.edugui.dao.impl.TeacherDaoImpl;
import com.ywb.edugui.dao.impl.UserDaoImpl;

/**
 * 工厂方法 + 单例设计模式 并且在使用synchronized关键词以最小代价处理并发
 * 懒汉模式：只在需要使用dao层的时候加载,避免内存开销，缺点是需要进行同步带来额外的开销。
 * :调用这个工厂的get方法返回相应的dao实现类
 */
public class DaoFactory {
    private volatile static UserDao userDao;
    private volatile static TeacherDao teacherDao;
    private volatile static SubjectDao subjectDao;
    private volatile static OssDao ossDao;
    /** 私有构造类，防止该类被实例化*/
    private  DaoFactory(){}
    public static UserDao getUserDao(){
        if(userDao == null){
            synchronized (DaoFactory.class){
                if(userDao == null){
                    userDao = new UserDaoImpl();
                }
            }
        }
        return userDao;
    }
    public static TeacherDao getTeacherDao(){
        if(teacherDao == null){
            synchronized (DaoFactory.class){
                if(teacherDao == null){
                    teacherDao = new TeacherDaoImpl();
                }
            }
        }
        return teacherDao;
    }
    public static SubjectDao getSubjectDao(){
        if(subjectDao == null){
            synchronized (DaoFactory.class){
                if(subjectDao == null){
                    subjectDao = new SubjectDaoImpl();
                }
            }
        }
        return subjectDao;
    }
    public static OssDao getOssDao(){
        if(ossDao == null){
            synchronized (DaoFactory.class){
                if(ossDao == null){
                    ossDao = new OssDaoImpl();
                }
            }
        }
        return ossDao;
    }

}
